package game.gui;

import org.lwjgl.input.Keyboard;

import engine.MADAL;
import engine.MADGL;
import engine.game.Level;
import engine.game.Manager;
import engine.gui.* ; 
import engine.input.IInput;
import engine.objects.GameObjects;
import engine.objects.ObjectInspector;
import engine.objects.PhysicsObject;
import game.objects.nonstaticObjects.EntityModel;

public class GUIManager extends Manager implements IInput{

	private static final long serialVersionUID = 1L;
	
	private StatusBar statusBar ; 
	private Inventory inventory ; 
	private InfoBar info ; 

	private final EntityModel entity ; 
	
	public GUIManager(GameObjects go, Level lvl, EntityModel entity) {
		super(go, lvl);
		this.entity = entity ; 
		init(); 
	}
	
	public StatusBar getStatusBar(){
		return this.statusBar ; 
	}
	
	public Inventory getInventory(){
		return this.inventory ; 
	}
	
	public boolean hasVisibleComponents(){
		for(PhysicsObject o: this.getObjects()){
			GUIObject g = (GUIObject)ObjectInspector.getInstance(o, GUIObject.class);
			if(g.isVisible() && g != this.statusBar && g != info)
				return true ; 
		}
		return false ; 
	}
	
	public GUIObject getFirstVisibleComponent(){
		for(PhysicsObject o: this.getObjects()){
			GUIObject g = (GUIObject)ObjectInspector.getInstance(o, GUIObject.class);
			if(g.isVisible() && g != this.statusBar) return g ;  
		}
		return null ;
	}
	
	public void init(){
		super.init(); 
		
		inventory = new Inventory(this, entity);
		statusBar = new StatusBar(this, entity) ; 
		info = new InfoBar(this, entity);
		
		this.addNewObject(inventory);
		this.addNewObject(statusBar);
		this.addNewObject(info);
	}

	@Override
	public void getInput() {
		while (Keyboard.next()) {
			if (Keyboard.getEventKeyState()) {
				if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) {
					if (this.getLevel().getGame().isPaused())
						this.getLevel().getGame().continueGame();
					else{
						if(this.hasVisibleComponents())
							this.getFirstVisibleComponent().hide();
						else
							this.getLevel().getGame().pause();
					}
				} else if (Keyboard.getEventKey() == Keyboard.KEY_I)
					GUIManager.changeVisibility(getInventory());
				else if (Keyboard.getEventKey() == Keyboard.KEY_F1)
					GUIManager.changeVisibility(getStatusBar());
				else if (Keyboard.getEventKey() == Keyboard.KEY_F3)
					GUIManager.changeVisibility(info);
//				else if(Keyboard.getEventKey() == Keyboard.KEY_F5)
//					new LevelSaveLoad().save(this.getLevel()); 
				else if (Keyboard.getEventKey() == Keyboard.KEY_F10)
					MADAL.quickPlay();
			}
		}
		for (PhysicsObject o : this.getObjects()) {
			if (ObjectInspector.isInstance(o, GUIObject.class)) {
				GUIObject o1 = (GUIObject) ObjectInspector.getInstance(o,
						GUIObject.class);
				o1.getInput();
			}
		}
	}
	
	public void update() {
		for (PhysicsObject o : this.getObjects()) {
			if (ObjectInspector.isInstance(o, GUIObject.class)) {
				GUIObject o1 = (GUIObject) ObjectInspector.getInstance(o, GUIObject.class);
				o1.update();
			}
		}
	}

	public void render(){
		super.render() ; 
	}
	
	public static void changeVisibility(GUIObject o){
		if(o.isVisible()) o.hide();
		else o.show() ; 
	}
	
}
